"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.crossService = exports.CrossService = void 0;
const log_1 = require("ee-core/log");
const ps_1 = require("ee-core/ps");
const path_1 = __importDefault(require("path"));
const axios_1 = __importDefault(require("axios"));
const utils_1 = require("ee-core/utils");
const cross_1 = require("ee-core/cross");
/**
 * cross
 * @class
 */
class CrossService {
    info() {
        const pids = cross_1.cross.getPids();
        log_1.logger.info('cross pids:', pids);
        let num = 1;
        pids.forEach(pid => {
            let entity = cross_1.cross.getProc(pid);
            log_1.logger.info(`server-${num} name:${entity.name}`);
            log_1.logger.info(`server-${num} config:`, entity.config);
            num++;
        });
        return 'hello electron-egg';
    }
    getUrl(name) {
        const serverUrl = cross_1.cross.getUrl(name);
        return serverUrl;
    }
    killServer(type, name) {
        if (type == 'all') {
            cross_1.cross.killAll();
        }
        else {
            cross_1.cross.killByName(name);
        }
    }
    /**
     * create go service
     * In the default configuration, services can be started with applications.
     * Developers can turn off the configuration and create it manually.
     */
    createGoServer() {
        return __awaiter(this, void 0, void 0, function* () {
            // method 1: Use the default Settings
            //const entity = await cross.run(serviceName);
            // method 2: Use custom configuration
            const serviceName = "go";
            const opt = {
                name: 'goapp',
                cmd: path_1.default.join((0, ps_1.getExtraResourcesDir)(), 'goapp'),
                directory: (0, ps_1.getExtraResourcesDir)(),
                args: ['--port=7073'],
                appExit: true,
            };
            const entity = yield cross_1.cross.run(serviceName, opt);
            log_1.logger.info('server name:', entity.name);
            log_1.logger.info('server config:', entity.config);
            log_1.logger.info('server url:', entity.getUrl());
        });
    }
    /**
     * create java server
     */
    createJavaServer() {
        return __awaiter(this, void 0, void 0, function* () {
            const serviceName = "java";
            const jarPath = path_1.default.join((0, ps_1.getExtraResourcesDir)(), 'java-app.jar');
            const opt = {
                name: 'javaapp',
                cmd: path_1.default.join((0, ps_1.getExtraResourcesDir)(), 'jre1.8.0_201/bin/javaw.exe'),
                directory: (0, ps_1.getExtraResourcesDir)(),
                args: ['-jar', '-server', '-Xms512M', '-Xmx512M', '-Xss512k', '-Dspring.profiles.active=prod', `-Dserver.port=18080`, `-Dlogging.file.path=${(0, ps_1.getLogDir)()}`, `${jarPath}`],
                appExit: false,
            };
            if (utils_1.is.macOS()) {
                // Setup Java program
                opt.cmd = path_1.default.join((0, ps_1.getExtraResourcesDir)(), 'jre1.8.0_201.jre/Contents/Home/bin/java');
            }
            if (utils_1.is.linux()) {
                // Setup Java program
            }
            const entity = yield cross_1.cross.run(serviceName, opt);
            log_1.logger.info('server name:', entity.name);
            log_1.logger.info('server config:', entity.config);
            log_1.logger.info('server url:', cross_1.cross.getUrl(entity.name));
        });
    }
    /**
     * create python service
     * In the default configuration, services can be started with applications.
     * Developers can turn off the configuration and create it manually.
     */
    createPythonServer() {
        return __awaiter(this, void 0, void 0, function* () {
            // method 1: Use the default Settings
            //const entity = await cross.run(serviceName);
            // method 2: Use custom configuration
            const serviceName = "python";
            const opt = {
                name: 'pyapp',
                cmd: path_1.default.join((0, ps_1.getExtraResourcesDir)(), 'py', 'pyapp'),
                directory: path_1.default.join((0, ps_1.getExtraResourcesDir)(), 'py'),
                args: ['--port=7074'],
                windowsExtname: true,
                appExit: true,
            };
            const entity = yield cross_1.cross.run(serviceName, opt);
            log_1.logger.info('server name:', entity.name);
            log_1.logger.info('server config:', entity.config);
            log_1.logger.info('server url:', entity.getUrl());
        });
    }
    requestApi(name, urlPath, params) {
        return __awaiter(this, void 0, void 0, function* () {
            const serverUrl = cross_1.cross.getUrl(name);
            const apiHello = serverUrl + urlPath;
            console.log('Server Url:', serverUrl);
            const response = yield (0, axios_1.default)({
                method: 'get',
                url: apiHello,
                timeout: 1000,
                params,
                proxy: false,
            });
            if (response.status == 200) {
                const { data } = response;
                return data;
            }
            return null;
        });
    }
}
exports.CrossService = CrossService;
CrossService.toString = () => '[class CrossService]';
const crossService = new CrossService();
exports.crossService = crossService;
